package com.hanggold.jfinal.excel.demo;

import com.hanggold.jfinal.excel.demo.mode.Student;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:sunmch@163.com">smc</a>
 * @date 2021-06-07 15:45
 * @since 1.0.0
 **/
public class ExcelDemo {
    
    /**
     * @功能：手工构建一个简单格式的Excel
     */
    private static List<Student> getStudent() throws Exception {
        List list = new ArrayList();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
        
        Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));
        Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
        Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
        list.add(user1);
        list.add(user2);
        list.add(user3);
        
        return list;
    }
    
    public static void main(String[] args) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个Excel文件
        HSSFCellStyle contextstyle = workbook.createCellStyle();
    
        HSSFSheet sheet = workbook.createSheet("学生表number");
        List<Student> list = getStudent();
        // 遍历集合数据，产生数据行
        for (int i = 0; i < list.size(); i++) {
            int rowIndex = i;
            HSSFRow contentRow = sheet.createRow(rowIndex);
            //            Map<String, Object> rowDate = outPutParam.getContent().get(i);
            Student student = list.get(i);
            HSSFCell contentCell = contentRow.createCell(0);
            contentCell.setCellValue(12);
            HSSFDataFormat df = workbook.createDataFormat(); // 此处设置数据格式
            contextstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));//数据格式只显示整数
            contentCell.setCellStyle(contextstyle);
           
        }
    
        try {
            FileOutputStream fout = new FileOutputStream("/Users/smc/data/test-data/easy-excel/students.xls");
            workbook.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        System.out.println("success");
    }
}
